#include "PluginConvertor.h"
#include "PluginException.h"
#include <boost/extension/shared_library.hpp>

namespace PluginSystem
{
	//! CreatePlugin
	/*!
		Funkcia sluziaca na vytvaranie instancii Converteru
	*/

	IPlugin *CreateConvertor(const char* thePluginPath)
	{
		return new PluginConvertor(thePluginPath);
	}

PluginConvertor::PluginConvertor(std::string thePluginPath)
{
	boost::extensions::shared_library myPlugin(thePluginPath); // otvorime plugin

	if(myPlugin.open())
	{
		//ak sa podari otvorit plugin na bind-ujeme vsetky funkcie
		_Open		= new boost::function<bool (PComunicator::PFileInfo&)>(myPlugin.get<bool,PComunicator::PFileInfo&>("Open"));
		_Format		= new boost::function<char* (char*)>(myPlugin.get<char*,char*>("Format"));
		_Read		= new boost::function<wchar_t* (char*)>(myPlugin.get<wchar_t*,char*>("Read"));
		_CreateHtml = new boost::function<char* (const char*)>(myPlugin.get<char*,const char*>("CreateHtml"));
		_Version	= new boost::function<char* (char*)>(myPlugin.get<char*,char*>("Version"));
		_Autor		= new boost::function<char* (char*)>(myPlugin.get<char*,char*>("Autor"));

		if( !_Open && !_Format && !_Read && !_CreateHtml && !_Version && !_Autor)
			throw PluginException("Missing functions in :"+thePluginPath);

	}
	else	//vyhodime vynimku
	{

		throw PluginException("Wrong or demaged plugin :"+thePluginPath);
	}
}

PluginConvertor::~PluginConvertor(void)
{
	delete _Autor;
	delete _CreateHtml;
	delete _Format;
	delete _Open;
	delete _Read;
	delete _Version;
}

bool PluginConvertor::Open(PComunicator::PFileInfo &theFile)
{
	return (*_Open)(theFile);
}

std::string PluginConvertor::Format(void)
{
	return (*_Format)("empty");
}

std::wstring PluginConvertor::Read(void)
{
	wchar_t *ret_val = (*_Read)("empty");
	std::wstring aString(ret_val);
	return aString;
}

std::string PluginConvertor::CreateHtml(std::string thePath)
{
	char *ret_val = (*_CreateHtml)(thePath.c_str());
	std::string string(ret_val);
	return string;
}

char* PluginConvertor::Version()
{
	return (*_Version)("empty");
}

char* PluginConvertor::Autor(void)
{
	return (*_Autor)("empty");
}

}//end of PluginSystem namespace
